//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.ruoyi.live.tool.utils;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.live.tool.commons.IAES;
import com.ruoyi.live.user.domain.ItmcCenterFile;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.Part;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class HttpPostUtils {
    public static HttpPostUtils getInstance() {
        return HttpPostUtils.SingletonHolder.instance;
    }

    public HttpPostUtils() {
    }

    public String sendFilePostByPath(Long id, String key, String token, String url, String path) {
        try {
            String sourcename = path.substring(path.lastIndexOf("/"));
            String filename = UUID.randomUUID().toString();
            File dist = new File(path);
            CloseableHttpClient client = null;
            CloseableHttpResponse response = null;

            String var15;
            try {
                HttpPost httpPost = new HttpPost(url);
                MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
                multipartEntityBuilder.addBinaryBody("file", dist);
                ItmcCenterFile obj = new ItmcCenterFile();
                obj.setFileCreateTime(System.currentTimeMillis());
                obj.setFileExt("");
                obj.setFileFolder("userHeaderImg");
                obj.setFileName(filename);
                obj.setFileSourceName(sourcename);
                obj.setFileToken(token);
                obj.setFilePid(id);
                obj.setFileType("userHeaderImg");
                multipartEntityBuilder.addTextBody("data", IAES.encryptToStr(JSON.toJSONString(obj), key));
                HttpEntity httpEntity = multipartEntityBuilder.build();
                httpPost.setEntity(httpEntity);
                client = HttpClients.createDefault();
                response = client.execute(httpPost);
                var15 = EntityUtils.toString(response.getEntity());
            } catch (Exception var20) {
                var20.printStackTrace();
                throw var20;
            } finally {
                if (response != null) {
                    response.close();
                }

                if (client != null) {
                    client.close();
                }

                dist.delete();
            }

            return var15;
        } catch (Exception var22) {
            var22.printStackTrace();
            return null;
        }
    }

    public String sendFilePost(Long id, String key, String token, String url, HttpServletRequest request) {
        try {
            Part part = request.getPart("file");
            InputStream is = part.getInputStream();
            String path = request.getSession().getServletContext().getRealPath("tmp/");
            File folder = new File(path);
            if (!folder.exists()) {
                folder.mkdir();
            }

            String disposition = part.getHeader("Content-Disposition");
            String sourcename = disposition.substring(disposition.lastIndexOf("="), disposition.length() - 1);
            sourcename = sourcename.substring(2);
            String suffix = disposition.substring(disposition.lastIndexOf("."), disposition.length() - 1);
            String filename = UUID.randomUUID().toString();
            FileOutputStream fos = new FileOutputStream(folder + "/" + filename);

            try {
                byte[] bty = new byte[1024];
                boolean var16 = false;

                int length;
                while((length = is.read(bty)) != -1) {
                    fos.write(bty, 0, length);
                }
            } catch (Exception var36) {
                var36.printStackTrace();
            } finally {
                fos.close();
                is.close();
            }

            File dist = new File(folder + "/" + filename);
            CloseableHttpClient client = null;
            CloseableHttpResponse response = null;

            String var22;
            try {
                HttpPost httpPost = new HttpPost(url);
                MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
                multipartEntityBuilder.addBinaryBody("file", dist);
                ItmcCenterFile obj = new ItmcCenterFile();
                obj.setFileCreateTime(System.currentTimeMillis());
                obj.setFileExt(suffix);
                obj.setFileFolder("userHeaderImg");
                obj.setFileName(filename);
                obj.setFileSourceName(sourcename);
                obj.setFileToken(token);
                obj.setFilePid(id);
                obj.setFileType("userHeaderImg");
                multipartEntityBuilder.addTextBody("data", IAES.encryptToStr(JSON.toJSONString(obj), key));
                HttpEntity httpEntity = multipartEntityBuilder.build();
                httpPost.setEntity(httpEntity);
                client = HttpClients.createDefault();
                response = client.execute(httpPost);
                var22 = EntityUtils.toString(response.getEntity());
            } catch (Exception var34) {
                var34.printStackTrace();
                throw var34;
            } finally {
                if (response != null) {
                    response.close();
                }

                if (client != null) {
                    client.close();
                }

                dist.delete();
            }

            return var22;
        } catch (Exception var38) {
            var38.printStackTrace();
            return null;
        }
    }

    public String sendPost(String data, String key, String token, String url) {
        try {
            CloseableHttpClient client = null;
            CloseableHttpResponse response = null;

            String var10;
            try {
                HttpPost httpPost = new HttpPost(url);
                httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
                List<NameValuePair> values = new ArrayList();
                values.add(new BasicNameValuePair("data", IAES.encryptToStr(data, key)));
                values.add(new BasicNameValuePair("token", token));
                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(values, "UTF-8");
                httpPost.setEntity(entity);
                client = HttpClients.createDefault();
                response = client.execute(httpPost);
                var10 = EntityUtils.toString(response.getEntity());
            } catch (Exception var15) {
                var15.printStackTrace();
                throw var15;
            } finally {
                if (response != null) {
                    response.close();
                }

                if (client != null) {
                    client.close();
                }

            }

            return var10;
        } catch (Exception var17) {
            var17.printStackTrace();
            return null;
        }
    }

    private static class SingletonHolder {
        private static final HttpPostUtils instance = new HttpPostUtils();

        private SingletonHolder() {
        }
    }
}
